package com.xinzhi.controller;

import com.xinzhi.anno.CheckRole;
import com.xinzhi.domain.User;
import com.xinzhi.service.CartService;
import com.xinzhi.service.LoginService;
import com.xinzhi.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
public class CartController {

    @Autowired
    CartService cartService;

    @Autowired
    LoginService loginService;

    // 添加商品到购物车
    @RequestMapping("/mtyx/addProductToCart")
    @CheckRole(role = {"user"})
    Result addProduct(String productId, HttpServletRequest request){
        User token = loginService.checkToken(request.getHeader("token"));
        if (token == null) return Result.error(5010,"token无效");

        return cartService.addProductToCart(String.valueOf(token.getUserId()),productId);
    }

    // 增加购物车商品数量   + 1
    @RequestMapping("/mtyx/plusProductToCart")
    @CheckRole(role = {"user"})
    Result plusProductToCart(String productId, HttpServletRequest request){
        User token = loginService.checkToken(request.getHeader("token"));
        if (token == null) return Result.error(5010,"token无效");

        return cartService.plusProductToCart(String.valueOf(token.getUserId()),productId);
    }

    // 减去购物车商品数量   - 1
    @RequestMapping("/mtyx/subtractProductToCart")
    @CheckRole(role = {"user"})
    Result subtractProductToCart(String productId, HttpServletRequest request){

        User token = loginService.checkToken(request.getHeader("token"));
        if (token == null) return Result.error(5010,"token无效");
        return cartService.subtractProductToCart(String.valueOf(token.getUserId()),productId);
    }

    // 删除购物车中的商品
    @RequestMapping("/mtyx/deleteProductToCart")
    @CheckRole(role = {"user"})
    Result deleteProductToCart(String productId, HttpServletRequest request){
        User token = loginService.checkToken(request.getHeader("token"));
        if (token == null) return Result.error(5010,"token无效");

        return cartService.deleteProductToCart(String.valueOf(token.getUserId()),productId);
    }

    // 修改购物车商品的选中状态
    @RequestMapping("/mtyx/changeProductSelected")
    @CheckRole(role = {"user"})
    Result changeProductSelected(String productId, HttpServletRequest request){
        User token = loginService.checkToken(request.getHeader("token"));
        if (token == null) return Result.error(5010,"token无效");

        return cartService.changeProductSelected(String.valueOf(token.getUserId()),productId);
    }

    // 修改购物车所有商品的选中状态（全选/全不选）
    @RequestMapping("/mtyx/changeAllProductSelected")
    @CheckRole(role = {"user"})
    Result changeAllProductSelected(HttpServletRequest request){
        User token = loginService.checkToken(request.getHeader("token"));
        if (token == null) return Result.error(5010,"token无效");

        return cartService.changeAllProductSelected(String.valueOf(token.getUserId()));
    }

    // 查看购物车
    @RequestMapping("/mtyx/checkCart")
    @CheckRole(role = {"user"})
    Result checkCart(HttpServletRequest request){
        User token = loginService.checkToken(request.getHeader("token"));
        if (token == null) return Result.error(5010,"token无效");

        return cartService.checkCartByUserId(String.valueOf(token.getUserId()));
    }






}
